package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class UserAuthority extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UserAuthority() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		final boolean isSU = Boolean.parseBoolean( request.getParameter("isSU") );
		final String userId = request.getParameter("userId");
		final String langId = "zh-TW";
		
		ArrayList<I2S3> fls = new ArrayList<I2S3>();
		
		String strSQL = null;
		
		try {
			conn = wid.getConn( true );
			
			errPars = wid.toJ("userId", userId) + wid.toJend("lagnId", langId);
			
			if ( isSU ) { 
				strSQL = "SELECT t3.roleid,t3.rolename,t2.funcid,t2.funcname,t4.pageurl FROM mprolefunc t1,funclang t2, rolelist t3,funclist t4 WHERE t1.roleid=t3.roleid AND t1.funcid=t4.funcid  AND t1.funcid=t2.funcid AND t2.lagnid='"+langId+"'  ORDER BY t3.roleid ASC,t2.funcid ASC";
			} else {
				strSQL = "SELECT t3.roleid,t3.rolename,t2.funcid,t2.funcname,t4.pageurl FROM mprolefunc t1,funclang t2, rolelist t3,funclist t4 WHERE t1.roleid=t3.roleid AND t1.funcid=t4.funcid AND t1.funcid=t2.funcid AND t2.lagnid='"+langId+"' AND t1.roleid in ( SELECT DISTINCT roleid FROM mproleuser WHERE userid='"+userId+"')  ORDER BY t3.roleid ASC,t2.funcid ASC";
			}
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			
			while ( rs.next() ) {
				I2S3 fl = new I2S3();
				fl.setIntA( rs.getInt("roleid") );
				fl.setIntB( rs.getInt("funcid") );
				fl.setStrA( wid.DB2UI( rs.getString("rolename"),conn.getMetaData().getDatabaseProductName() ) );
				fl.setStrB( wid.DB2UI( rs.getString("funcName"),conn.getMetaData().getDatabaseProductName() ) );
				fl.setStrC( rs.getString("pageurl") );
				fls.add( fl );
			}
		} catch (SQLException e) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch (Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if ( rs != null ) { rs.close(); }
				if ( ps!= null ) { ps.close(); }
				if ( conn!= null ) { conn.close(); }
			} catch (SQLException e) {
				System.out.println( wid.errProcess(e) );
			}
		}
		
		response.getWriter().write( new Gson().toJson(fls) );	
	}
}